package co.edu.usbcali.ai.ga.bl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import co.edu.usbcali.ai.ga.Individual;

import com.cgarias.utils.Random;


public class DetTournamentChooser implements IChooser {
	
	private static DetTournamentChooser instance=new DetTournamentChooser();

	int cutType;
	int totalCuts;

	public static IChooser getInstance() {
		return instance;
	}

	
	/**
	 * Escoje los mejores mediante la tecnica de torneo
	 */
	public List<Individual> choose(List<Individual> list, int winners) {
		List<Individual> copyList=new ArrayList<Individual>(list);
		List<Individual> winnersList=new ArrayList<Individual>(winners);
		int groupSize=(int)list.size()/winners;
		for(int con=0;con<winners;con++){
			List<Individual> groupList;
			if (copyList.size()>=groupSize*2){
				groupList=makeGroup(copyList, groupSize);
				Collections.sort(groupList);
				winnersList.add(groupList.get(groupSize-1));
			}else{				
//				groupList=makeGroup(copyList, copyList.size());
				groupList=copyList;
				Collections.sort(groupList);
				winnersList.add(groupList.get(copyList.size()-1));
			}
		}
		return winnersList;	
	}

	
	public List<Individual> makeGroup(List<Individual> waitingList,int size){
		List<Individual> list=new ArrayList<Individual>(size);
		for (int con=0;con<size;con++){
			list.add((Individual)waitingList.remove(Random.random(size-con)));
		}
		return list;
	}
	
	
}
